﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class Model : System.Web.UI.Page
{
    string maHang;
    string modelid;
    protected void Page_Load(object sender, EventArgs e)
    {        
        //if (!IsPostBack)
        //{
        //    MultiView1.ActiveViewIndex = 0;
        //    DisplayList();
        //}
        if (!IsPostBack)
        {
            if (Session["AdminName"] != null)
            {
                DropDownList1.Items.Clear();
                string sql = "select TenHang from hangxe";
                DataTable dt = DataBase.GetDataTable(sql);
                foreach (DataRow dtr in dt.Rows)
                {
                    DropDownList1.Items.Add(dtr["TenHang"].ToString());
                }
                MultiView1.ActiveViewIndex = 0;
                DisplayList();
            }
            else
                Response.Redirect("Default.aspx");
        }     
    }

    protected void DisplayList()
    {
        string sql = string.Format(@"Select 
                                            a.TenHang,
                                            b.ModelID,
                                            b.Model 
                                    From HangXe as a INNER JOIN model as b 
                                                     ON a.MaHangXe=b.MaHangXe
                                    ");
        DataTable dt = DataBase.GetDataTable(sql);
        GridView1.DataSource = dt;
        GridView1.DataBind();
        MultiView1.ActiveViewIndex = 0;
    }

    protected void DisplayListEdit(string modelid)
    {
        string sql = string.Format(@"Select 
                                            a.TenHang,
                                            b.ModelID,
                                            b.Model 
                                    From HangXe as a INNER JOIN model as b 
                                                     ON a.MaHangXe=b.MaHangXe
                                    WHERE ModelID='{0}'
                                    ", modelid);
        DataTable dt = DataBase.GetDataTable(sql);
        foreach (DataRow dtr in dt.Rows)
        {
            DropDownList1.Text = dtr["TenHang"].ToString();
            txtModelID.Text = dtr["ModelID"].ToString();
            txtmodel.Text = dtr["Model"].ToString();
        }
    }

    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        string modelID = GridView1.Rows[e.RowIndex].Cells[3].Text;
        string sql = "delete from model where ModelID='" + modelID + "'";
        try
        {
            DataBase.ExecuteNonQuery(sql);
            DisplayList();
        }
        catch (Exception ex)
        {
            Response.Write(ex);
        }
    }

    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        MultiView1.ActiveViewIndex = 1;
        string modelID = GridView1.Rows[e.NewEditIndex].Cells[3].Text;
        this.DisplayListEdit(modelID);
        DropDownList1.Enabled = false;
        txtModelID.ReadOnly = true;
        btnUpdate.Text = "Update";
    }

    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {

    }

    protected void btnThemMoi_Click(object sender, EventArgs e)
    {
        DropDownList1.Enabled = true;
        txtModelID.Text = "";
        txtmodel.Text = "";
        btnUpdate.Text = "Add";
        MultiView1.ActiveViewIndex = 1;
    }
    protected void btnList_Click(object sender, EventArgs e)
    {
        DisplayList();
    }

    protected void btnUpdate_Click(object sender, EventArgs e)
    {
        if (btnUpdate.Text.Equals("Add"))
        {
            string tenhang = DropDownList1.SelectedItem.Text;
            getMaHang(tenhang);
            modelid = maHang;
            modelid = modelid + txtmodel.Text;
            txtModelID.Text = modelid;
            string sql = string.Format(@"
                                        INSERT INTO model
                                            (
                                                ModelID,
                                                MaHangXe,
                                                Model
                                            )
                                        VALUES
                                            (
                                                '{0}',
                                                '{1}',
                                                '{2}'
                                            )
                                        ", txtModelID.Text,
                                         maHang,
                                         txtmodel.Text);
            DataBase.ExecuteNonQuery(sql);
            DisplayList();
        }
        if (btnUpdate.Text.Equals("Update"))
        {
            string sql = string.Format(@"
                                        UPDATE model
                                        SET Model='{0}'
                                        WHERE ModelId='{1}'
                                        ", txtmodel.Text,
                                         txtModelID.Text);
            try
            {
                DataBase.ExecuteNonQuery(sql);
                DisplayList();
            }
            catch (Exception ex)
            {
                Response.Write(ex);
            }
                                            
        }
    }

    private void getMaHang(string tenHang)
    {
        string sql = "select MaHangXe from hangxe where TenHang='" + tenHang + "'";
        DataTable dt = DataBase.GetDataTable(sql);
        foreach(DataRow dtr in dt.Rows)
        {
            maHang = dtr["MaHangXe"].ToString();
        }
    }

    protected void txtmodel_TextChanged(object sender, EventArgs e)
    {
        
    }
    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridView1.PageIndex = e.NewPageIndex;
        this.DisplayList();
    }
}
